package devbureau.fstore.common.load.impl;

import devbureau.fstore.common.Item;
import devbureau.fstore.common.load.FileParser;
import java.io.ByteArrayInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
//import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class XLSFileParser implements FileParser{

	static Logger logger = Logger.getLogger(XLSFileParser.class);

    
    public List<Item> parse(byte[] fileData) throws Exception {
        logger.debug("#parse enter");
        ArrayList<Item> items = new ArrayList<Item>();

        HSSFWorkbook wb     = new HSSFWorkbook(new ByteArrayInputStream(fileData));
        HSSFSheet sheet = wb.getSheetAt(0);       // first sheet

        logger.debug("#parse sheet[0] name = " + sheet);
        for(int i = sheet.getFirstRowNum();i<= sheet.getLastRowNum();i++){
            HSSFRow row     = sheet.getRow(i);
            logger.debug("#parse row = " + row);
            if(row!= null) {
                String data[] = new String[row.getLastCellNum()-row.getFirstCellNum()];
                int jj = 0;
                String valueSum = "";
                for(int j=row.getFirstCellNum();j< row.getLastCellNum();j++){
                    String value = null;
                    if(row.getCell(j) != null) {

                        if(! HelperFileParser.emptyValues.contains(row.getCell(j).toString())) {
                            if(jj == HelperFileParser.DELIVERYDATE_POS){
                                value = new SimpleDateFormat(HelperFileParser.DELIVERYDATE_FORMAT).format(row.getCell(j).getDateCellValue());
                            }else
                            if(jj == HelperFileParser.LENGTH_POS){
                                value = new Integer((int)row.getCell(j).getNumericCellValue()).toString();
                            }else
                            if(jj == HelperFileParser.QUANTITY_POS){
                                value = new Integer((int)row.getCell(j).getNumericCellValue()).toString();
                            }else {
                                value = row.getCell(j).toString();
                            }
                        }else{
                            value = row.getCell(j).toString();
                        }

                    }else{
                        value = "";
                    }

                    valueSum += value;
                    data[jj] = value;
                    jj++;
                }

                if(!valueSum.isEmpty()) {
                    Item item = HelperFileParser.parseData(data);
                    items.add(item);
                }else{
                    logger.debug("#parse row is empty");
                }
            }
        }
        logger.debug("#parse exit");
        return items;
    }
}
